<template>
    <div class="app-container">
      <el-row :gutter="10" class="mb5">
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:record:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
        </el-col>
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" class="el-form-search">
                          <el-form-item label="连接名称" prop="connName" class="el-form-search-item">
                              <el-input
                                      v-model="queryParams.connName"
                                      placeholder="请输入连接名称"
                                      clearable
                                      size="mini"
                                      @keyup.enter.native="handleQuery"
                              />
                          </el-form-item>
                          <el-form-item label="任务名称" prop="taskName" class="el-form-search-item">
                              <el-input
                                      v-model="queryParams.taskName"
                                      placeholder="请输入任务名称"
                                      clearable
                                      size="mini"
                                      @keyup.enter.native="handleQuery"
                              />
                          </el-form-item>

                          <el-form-item label="状态" prop="status" class="el-form-search-item">
                              <el-select
                                      v-model="queryParams.status"
                                      clearable
                                      size="mini">
                                    <el-option
                                            v-for="item in [{val:'success',label:'成功'}, {val:'fail',label:'失败'}]"
                                            :key="item.val"
                                            :value="item.val"
                                            :label="item.label"></el-option>
                                    </el-select>
                          </el-form-item>


              <el-form-item class="el-form-search-item">
                  <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                  <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
          </el-form>
      </el-row>
  
      <el-table :height="tableHeight" v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键" align="center" prop="id" width="50" />
        <el-table-column label="连接名称" align="center" prop="connName" width="100" />
        <el-table-column label="开始时间" align="center" prop="startTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束时间" align="center" prop="endTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
       
        <el-table-column label="状态" align="center" prop="status" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status==='success'" type="success">成功</el-tag>
            <el-tag v-else type="danger">失败</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-zoom-in"
              @click="handleview(scope.row)"
            >查看</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:record:remove']"
            >删除</el-button>

            <el-button
              size="mini"
              type="text" v-if="scope.row.status==='fail'"
              icon="el-icon-error"
              @click="handleerrMsg(scope.row)"
            >错误信息</el-button>

          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改数据备份记录对话框 -->
      <el-dialog title="错误日志" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false" v-dialogDrag>
        <el-form ref="form" :model="form"  label-width="80px">
                   <!-- 显示恢复消息 -->
                   <el-form-item label="错误消息" prop="log">
                    <el-input ref="recoveryMsgTextarea" type="textarea" v-model="form.errMsg" :auto-rows="true" readonly
                        :rows="10"></el-input>
                </el-form-item>
        </el-form>
      </el-dialog>


      <el-dialog title="备份详情" :visible.sync="opendetail" width="800px" append-to-body :close-on-click-modal="false" v-dialogDrag>
        <tablesFile ref="tablesFile"></tablesFile>
      </el-dialog>



    </div>
  </template>
  
  <script>
  import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/db/record";

  import tablesFile from "../tableFile/index";

  
  export default {
    name: "Record",
    components: {
      tablesFile
    },
    data() {
      return {
        // 表格高度
        tableHeight: document.documentElement.clientHeight - 180,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 数据备份记录表格数据
        recordList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20,
          orderByColumn: "id",
          isAsc: "desc",
          connName: null,
          status: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },

        opendetail:false,
      };
    },
    created() {
      this.getList();
    },


    watch: {
        'form.errMsg': {
            handler(newValue) {
                this.$nextTick(() => {
                    const textarea = this.$refs.recoveryMsgTextarea.$el.firstChild;
                    if (textarea) {
                        textarea.scrollTop = textarea.scrollHeight;
                    }
                });
            },
            deep: true
        }
    },


    methods: {
      /** 查询数据备份记录列表 */
      getList() {
        this.loading = true;
        listRecord(this.queryParams).then(response => {
          this.recordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          connName: null,
          startTime: null,
          endTime: null,
          id: null,
          status: null,
          errMsg: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加数据备份记录";
      },
      /** 查看错误 */
      handleerrMsg(row) {
        this.form={...row};
        this.open = true;
      },

       /** 删除按钮操作 */
       handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('删除记录将会同步删除备份文件？').then(function() {
          return delRecord(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },


      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateRecord(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addRecord(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleview(row) {
        let tableList=JSON.parse(row.tablesToFile);
        console.log("表格数据",tableList);
        this.opendetail=true;
        this.$nextTick(()=>{
          this.$refs.tablesFile.setData(tableList);
        })

      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('db/record/export', {
          ...this.queryParams
        }, `record_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  